﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Designer
{
	public class GeneralTableCreator
	{
		public static TableInfos CreateTableInfos(Autodesk.Revit.DB.Document doc, int rowCount, int colCount, List<CellInfo> cellInfos, List<double> rowHeightList = null, List<double> colWidthList = null)
		{
			TableInfos result = null;
			string text = "";
			if (!GeneralTableCreator.CreateTableInfos(doc, rowCount, colCount, cellInfos, ref result, ref text, rowHeightList, colWidthList))
			{
				return null;
			}
			return result;
		}

		private static bool CreateTableInfos(Autodesk.Revit.DB.Document doc, int rowCount, int colCount, List<CellInfo> cellInfos, ref TableInfos tableInfos, ref string message, List<double> rowHeightList = null, List<double> colWidthList = null)
		{
			TableHead tableHeadData = null;
			if (!GeneralTableCreator.CreateTableHead(rowCount, colCount, cellInfos, ref tableHeadData, rowHeightList, colWidthList))
			{
				return false;
			}
			TableBody tableBodyData = null;
			if (!GeneralTableCreator.CreateTableBody(ref tableBodyData))
			{
				return false;
			}
			TextNoteType labelTextNoteType = GeneralTableCreator.GetLabelTextNoteType(doc);
			double hMargin = 1.0;
			double vMargin = 1.5;
			tableInfos = new TableInfos(tableHeadData, tableBodyData, labelTextNoteType, hMargin, vMargin, true);
			return true;
		}

		private static TextNoteType GetLabelTextNoteType(Autodesk.Revit.DB.Document doc)
		{
			string strB = "宋体 3mm";
			string text = "BrickStatisticalResult";
			double num = Common.MMToFeet(3.5);
			if (RevitVersion.Version != "2016")
			{
				num = Common.MMToFeet(2.9);
			}
			TextNoteType textNoteType = null;
			TextNoteType textNoteType2 = null;
			TextNoteType textNoteType3 = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(TextNoteType));
			FilteredElementIterator elementIterator = filteredElementCollector.GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				TextNoteType textNoteType4 = element as TextNoteType;
				if (textNoteType4 != null)
				{
					if (textNoteType4.Name.CompareTo(text) == 0)
					{
						textNoteType = textNoteType4;
						break;
					}
					if (textNoteType2 == null && textNoteType4.Name.CompareTo(strB) == 0)
					{
						textNoteType2 = textNoteType4;
					}
					textNoteType3 = textNoteType4;
				}
			}
			if (textNoteType == null)
			{
				if (textNoteType3 == null)
				{
					return null;
				}
				if (textNoteType2 == null)
				{
					textNoteType2 = textNoteType3;
				}
				Transaction transaction = new Transaction(doc);
				transaction.Start("new textnoteType");
				textNoteType = (textNoteType2.Duplicate(text) as TextNoteType);
				double num2 = num;
				textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(num2);
				string text2 = "FangSong_GB2312";
				textNoteType.get_Parameter(BuiltInParameter.TEXT_FONT).Set(text2);
				double num3 = 0.7;
				textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).Set(num3);
				double num4 = Common.MMToFeet(8.0);
				textNoteType.get_Parameter(BuiltInParameter.TEXT_TAB_SIZE).Set(num4);
				transaction.Commit();
			}
			return textNoteType;
		}

		private static bool CreateTableHead(int rowCount, int colCount, List<CellInfo> cellInfos, ref TableHead tableHead, List<double> rowHeightList = null, List<double> colWidthList = null)
		{
			TableHead._Tag_HeadType tableHeadType = TableHead._Tag_HeadType.HT_NULL;
			int sheetIndex = 0;
			int badyStartRowNumber = 0;
			int badyEndRowNumber = 60;
			TableSourceInfo tableSourceInfo = new TableSourceInfo("", sheetIndex, badyStartRowNumber, badyEndRowNumber);
			List<int> list = new List<int>();
			for (int i = 0; i < colCount; i++)
			{
				list.Add(1);
			}
			List<TableHeadCell> list2 = new List<TableHeadCell>();
			foreach (CellInfo cellInfo in cellInfos)
			{
				int rowCount2 = cellInfo.EndRowIndex - cellInfo.StartRowIndex + 1;
				int num = cellInfo.EndColIndex - cellInfo.StartColIndex + 1;
				list2.Add(GeneralTableCreator.CreateColumnCell(cellInfo.StartRowIndex, rowCount2, cellInfo.StartColIndex, num, cellInfo));
				int num2 = (int)Math.Ceiling((double)cellInfo.Text.Length / ((double)num * 1.0));
				for (int j = cellInfo.StartColIndex; j <= cellInfo.EndColIndex; j++)
				{
					if (list[j] < num2)
					{
						list[j] = num2;
					}
				}
			}
			double num3 = Common.MMToFeet(10.0);
			if (rowHeightList == null)
			{
				rowHeightList = new List<double>();
				for (int i = 0; i < rowCount; i++)
				{
					rowHeightList.Add(num3);
				}
			}
			if (colWidthList == null)
			{
				colWidthList = new List<double>();
				for (int i = 0; i < colCount; i++)
				{
					colWidthList.Add(num3 / 3.0 * (double)list[i]);
				}
			}
			tableHead = new TableHead(tableHeadType, tableSourceInfo, list2, rowCount, colCount, rowHeightList, colWidthList);
			return true;
		}

		private static TableHeadCell CreateColumnCell(int rowIndex, int rowCount, int columnIndex, int colCount, CellInfo cellInfo)
		{
			List<int> list = new List<int>();
			for (int i = 0; i < rowCount; i++)
			{
				list.Add(rowIndex + i);
			}
			List<int> list2 = new List<int>();
			for (int i = 0; i < colCount; i++)
			{
				list2.Add(columnIndex + i);
			}
			TableHeadCellKey key = new TableHeadCellKey();
			return new TableHeadCell(list, list2, cellInfo.Text, key, (TableHeadCell.CellInfoType)cellInfo.InfoType);
		}

		private static bool CreateTableBody(ref TableBody tableBody)
		{
			List<TableBodyRow> bodyData = new List<TableBodyRow>();
			tableBody = new TableBody(bodyData);
			return true;
		}
	}
}
